import { reqAddOrUpdateShopCar, reqGoodsInfo } from "@/api"
//封装游客身份模块
import {getUUID} from '@/utils/uuid_token.js'

const state = {
    goodInfo: {},
    //游客临时身份
    uuid_token:getUUID()
}
const mutations = {
    GETGOODINFO(state, goodInfo) {
        state.goodInfo = goodInfo
    }
}
const actions = {
    //货群产品、 信息的action
    async getGoodInfo({ commit }, skuId) {
        let result = await reqGoodsInfo(skuId)
        if (result.code == 200) {
            commit("GETGOODINFO", result.data)
        }
    },
    //将产品添加到购物车中
   async addOrUpdateShopCar({commit},{skuId,skuNum}){
    //加入购物车返回的结果
    //加入购物车后,前台将参数带给服务器
    //服务器写入数据成功,并没有返回多余数据
      let result =  await reqAddOrUpdateShopCar(skuId,skuNum)
      //代表服务器加入成功
      if (result.code == 200) {
        return "ok";
      }else{
        return Promise.reject(new Error('fail'))
      }
    }
}
const getters = {
    categoryView(state) {
        //当前计算出的属性值至少是一个空对象
        return state.goodInfo.categoryView || {};
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {};
    },
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || [];
    }
}


export default{
        state,
        mutations,
        actions,
        getters
    }